#include <QApplication>
#include <QVideoWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include<QObject>
#include<Windows.h>
#include<WinUser.h>
#include<iostream>
#pragma comment(lib,"user32.lib")

HWND _workerw = nullptr;

inline BOOL CALLBACK EnumWindowsProc(_In_ HWND tophandle, _In_ LPARAM topparamhandle)
{
    HWND defview = FindWindowEx(tophandle, 0, L"SHELLDLL_DefView", nullptr);
    if (defview != nullptr)
    {
        _workerw = FindWindowEx(0, tophandle, L"WorkerW", 0);
    }
    return true;
}//遍历句柄的回调函数

HWND GetWorkerW(){
    int result;
    HWND windowHandle = FindWindow(L"Progman", nullptr);
    SendMessageTimeout(windowHandle, 0x052c, 0 ,0, SMTO_NORMAL, 0x3e8,(PDWORD_PTR)&result);
    EnumWindows(EnumWindowsProc,(LPARAM)nullptr);
//    ShowWindow(_workerw,SW_HIDE);
    return windowHandle;
}//获取桌面最底层的句柄

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMediaPlayer* player = new QMediaPlayer;
    QVideoWidget *videoWidget=new QVideoWidget;
    QMediaPlaylist* playlist=new QMediaPlaylist;
    videoWidget->setWindowFlag(Qt::FramelessWindowHint);
    playlist->clear();
    playlist->addMedia(QUrl::fromLocalFile("poppy.wmv"));
    //播放列表加入视频
    videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);//视频填充整个窗口
    player->setVideoOutput(videoWidget);//播放器设置播放窗口
    player->setPlaylist(playlist);//播放器导入播放列表
    videoWidget->setFullScreen(true);//全屏
    playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);//播放列表设置循环播放
    SetParent((HWND)videoWidget->winId(), GetWorkerW());
    //将视频句柄附加到桌面句柄上
    player->play();
    QObject::connect(player,&QMediaPlayer::positionChanged,[&](qint64 t){
        if(t>=9000)
        {
            player->setPosition(10);
            player->play();
        }

    });//通过信号槽设置AB循环，防止出现黑影
    videoWidget->show();
    return a.exec();
}
